<template>
  <div class="list-container">
    <vab-query-form>
      <el-form
        ref="form"
        :model="queryForm.queryBean"
        :inline="true"
        @submit.native.prevent
      >
        <el-form-item>
          <el-select
            v-model.trim="queryForm.queryBean.rootPartId"
            collapse-tags
            :style="{ width: '100%' }"
            placeholder="请选择类别"
            clearable
          >
            <el-option
              v-for="item in estateOptions"
              :key="item.partId"
              :label="item.partName"
              :value="item.partId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model.trim="queryForm.queryBean.queryStartTime"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd HH:mm:ss"
            :style="{ width: '100%' }"
            placeholder="开始时间"
            clearable
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model.trim="queryForm.queryBean.queryEndTime"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd HH:mm:ss"
            :style="{ width: '100%' }"
            placeholder="结束时间"
            clearable
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            icon="el-icon-search"
            type="primary"
            native-type="submit"
            @click="handleQuery"
          >
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </vab-query-form>
    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      :element-loading-text="elementLoadingText"
      :height="height"
    >
      <el-table-column
        show-overflow-tooltip
        prop="propertyName"
        label="地产名称"
        width="120"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="归属"
        prop="projectName"
        width="120"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="进度值"
        prop="progressValue"
        width="120"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="审核状态"
        prop="progressStatus"
        width="120"
      >
        <template #default="{ row }">
          <el-tag
            size="small"
            :type="
              row.progressStatus == 3
                ? 'success'
                : row.progressStatus == 4
                ? 'danger'
                : row.progressStatus == 1
                ? 'info'
                : 'warning'
            "
          >
            {{ getValue(row.progressStatus) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="创建时间"
        prop="createTime"
      ></el-table-column>
      <el-table-column show-overflow-tooltip label="审核" width="200">
        <template #default="{ row }">
          <el-button type="primary" @click="handleEdit(row)">详情</el-button>
          <!-- <el-button
            v-if="row.progressStatus == 2 || row.progressStatus == 11"
            type="primary"
            size="mini"
            @click="handlePass(row)"
          >
            通 过
          </el-button>
          <el-button
            v-if="row.progressStatus == 2 || row.progressStatus == 11"
            type="danger"
            size="mini"
            @click="handleRefuse(row)"
          >
            不通过
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <check-progress ref="check" @fetch-data="fetchData"></check-progress>
  </div>
</template>
<script>
  import { getProgressList, updateProgress, getConstantId } from '@/api/checked'
  import _ from 'lodash'
  import CheckProgress from './components/CheckProgress.vue'
  export default {
    name: 'Progress',
    components: { CheckProgress },
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        selectRows: '',
        elementLoadingText: '正在加载...',
        estateOptions: [],
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          queryBean: {
            //    progressStatus: 0,
            // progressValue: 0,
            // projectName: '',
            // queryEndTime: '',
            // queryStartTime: '',
            // rootPartId: '',
          },
        },
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.init()
      this.fetchData()
    },
    mounted() {},
    methods: {
      async init() {
        const { data } = await getConstantId()
        if (data.status == 0) {
          this.estateOptions = data.body
        } else {
        }
      },
      handleEdit(row) {
        if (row) {
          this.$refs['check'].showEdit(row)
        } else {
          this.$refs['check'].showEdit()
        }
      },
      // progressStatus (integer, optional): 审核状态 1 -未审核 2-通过审核 3-审核失败 ,
      getValue(value) {
        switch (value) {
          case 1:
            return '未上传'
          case 2:
            return '待审核'
          case 3:
            return '已通过'
          default:
            return '审核失败'
        }
      },
      async handlePass(row) {
        let temp = _.cloneDeep(row)
        temp.progressStatus = 3
        const { data } = await updateProgress(temp)
        this.fetchData()
      },
      async handleRefuse(row) {
        let temp = _.cloneDeep(row)
        temp.progressStatus = 4
        const { data } = await updateProgress(temp)
        this.fetchData()
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      handleQuery() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        console.log(this.queryForm)
        const { data } = await getProgressList(this.queryForm)
        const { body } = data
        console.log(body.data)
        this.list = body.data
        // data.forEach((item, index) => {})
        this.total = body.totalSize
        this.$baseNotify('进度数据获取完毕！', '成功', 'success', 'top-right')
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      },
    },
  }
</script>
